package com.wan.tool;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.net.CacheRequest;
import java.util.Calendar;
import java.util.List;

public class InAndOut {

    public static SharedPreferences shared;
    public static Editor editer;

    public final static int TASK_CET4 = 8;
    public final static int TASK_CET6 = 9;
    public final static int TASK_xgn1 = 1;
    public final static int TASK_xgn2 = 2;
    public final static int TASK_xgn3 = 3;
    public final static int TASK_xgn4 = 4;
    public final static int TASK_GRE = 5;
    public final static int TASK_TUOFU = 6;
    public final static int TASK_JISUANJI = 7;
    public final static int TASK_YASI = 10;
    public final static int TASK_YANJIUSHENG = 11;
    public final static int TASK_YANJIUSHENGHEXING = 12;


    public static void init(SharedPreferences sharedPreferences) {
        shared = sharedPreferences;
        editer = shared.edit();
    }

    public static void delTask() {
        editer.clear();
        editer.commit();
    }


    public static class TaskInfo {
        public int wordCount;
        public int start; //任务起点，从0-(max-1)开始
        public int dayCount; //一天记多少 ，
        public int nowDay;//完成了几天的任务，从1开始
        public int nowIndex;//今天任务已经完成了几个
        public int taskId;
        public int nowTimes;
        public int nowToday = -1;


        public String taskName;
        public List<Word> wordList;


        //今天需要背的单词数量
        public int getTodayNeedPei() {
            int lastword = wordCount - (dayCount * nowDay) - start;
            return lastword > dayCount ? dayCount : lastword;
        }

        //今天已经背了的
        public int getTodayYiJinPei() {
            return nowIndex;
        }

        //获取任务的名字
        public String getTaskName() {
            return taskName;
        }

        //获得这个任务需要的天数
        public int getTaskNeedDay() {
            return (wordCount - start + dayCount - 1) / dayCount;
        }

        //已经完成了几天
        public int getTaskYijinDay() {
            if (nowTimes >= 3) {
                return nowDay + 1;
            }
            return nowDay;
        }

        //今天完成了几次
        public int getNowTimes() {
            return nowTimes;
        }

        //单词总数
        public int getTaskWordCount() {
            return wordCount;
        }

        //查看已经背诵的单词数量
        public int getTaskYijinWord() {
            if (nowTimes >= 1) {
                return start + dayCount * nowDay + getTodayNeedPei();
            } else {
                return start + dayCount * nowDay + nowIndex;
            }

        }

        public Word nextWord() {
            if (nowIndex >= wordList.size()) {
                Log.i("changnowtimes",Thread.currentThread().getName()+":"+nowIndex+":"+nowTimes+":"+this);
                //今天的结束一遍
                nowIndex = 0;
                nowTimes = shared.getInt("nowtimes" + taskId, 0) + 1;
                editer.putInt("nowindex" + taskId, nowIndex);
                editer.putInt("nowtimes" + taskId, nowTimes);
                editer.commit();
                return null;
            } else {
                Word word = wordList.get(nowIndex++);
                editer.putInt("nowindex" + taskId, nowIndex);
                editer.commit();
                return word;
            }
        }

        public Word olnyGetNowWord() {
            if(wordList==null){
                loadWord();
            }
            if (nowIndex >= wordList.size()) {
                return null;
            }
            Word word = wordList.get(nowIndex);
            return word;
        }

        public boolean isAllFinish() {
            if (isFinishToday() && (dayCount * (nowDay + 1)) + start >= this.wordCount) {
                return true;
            }
            if (dayCount * nowDay + start >= wordCount) {
                return true;
            }
            return false;
        }

        public static boolean staticIsAllFinish(int s_nowTimes,int s_dayCount,int s_nowDay,int s_start ,int s_wordCount){
            if (s_nowTimes >= 3 && (s_dayCount * (s_nowDay + 1)) + s_start >= s_wordCount) {
                return true;
            }
            if (s_dayCount * s_nowDay + s_start >= s_wordCount) {
                return true;
            }
            return false;

        }


        public boolean isFinishToday() {
            //已经背诵了三次再进入结果页
            if (nowTimes >= 3) {
                return true;
            }
            return false;
        }

        public void goTomorrow() {
            nowIndex = 0;
            editer.putInt("nowindex" + taskId, nowIndex);
            nowDay = nowDay + 1;
            editer.putInt("nowday" + taskId, nowDay);
            nowTimes = 0;
            editer.putInt("nowtimes" + taskId, nowTimes);
            editer.commit();
        }


        public void loadWord() {
            wordList = Word.getWordList(start + nowDay * dayCount, dayCount, taskId);
        }

        public static int getDefaultTaskId() {
            return shared.getInt("alive", 0);
        }

        public static void goOnTaskInfo(int taskId) {
            editer.putInt("alive", taskId);
            editer.commit();
        }

        public static void createTaskInfo(int taskId, int start, int dayCount) {
            editer.putInt("start" + taskId, start);
            editer.putInt("daycount" + taskId, dayCount);
            editer.putInt("nowday" + taskId, 0);//完成了几天的任务
            editer.putInt("nowindex" + taskId, 0);//今天任务完成了几个
            editer.putInt("nowtoday" + taskId, Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
            editer.putInt("nowtimes" + taskId, 0);
            editer.putBoolean("isfinish" + taskId, false);
            editer.putInt("alive", taskId);
            editer.commit();
        }

        public static TaskInfo getTaskInfo(int taskId) {
            if (taskId == 0) {
                taskId = TaskInfo.getDefaultTaskId();
            }
            TaskInfo taskInfo = getTaskInfoFromDB(taskId);
            TaskInfo taskInfo1 = getTaskInfoFromXml(taskId);
            if (taskInfo != null && taskInfo.taskId != 0) {
                if (taskInfo1 != null && taskInfo1.dayCount != 0) {
                    taskInfo.dayCount = taskInfo1.dayCount;
                    taskInfo.nowIndex = taskInfo1.nowIndex;
                    taskInfo.start = taskInfo1.start;
                    taskInfo.nowDay = taskInfo1.nowDay;
                    taskInfo.nowTimes = taskInfo1.nowTimes;
                    taskInfo.nowToday = taskInfo1.nowToday;
                }
            }
            if (taskInfo == null || taskInfo.taskId == 0 || taskInfo.wordCount == 0) {
                return null;
            }
            return taskInfo;
        }

        private static TaskInfo getTaskInfoFromXml(int taskId) {
            if (taskId != 0) {
                int start = shared.getInt("start" + taskId, 0);
                int dayCount = shared.getInt("daycount" + taskId, 0);
                int nowDay = shared.getInt("nowday" + taskId, 0);//完成了几天的任务
                int nowToday = shared.getInt("nowtoday" + taskId, -1);
                int nowTimes = shared.getInt("nowtimes" + taskId, 0);
                int nowIndex = shared.getInt("nowindex" + taskId, 0);//今天任务完成了几个
                boolean isFinish = shared.getBoolean("isfinish" + taskId, false);
                if (!isFinish) {
                    int today = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
                    if (today != nowToday) {
                        //昨天任务完成了的话就开始新的一天。
                        //认定条件，背诵3遍
                        if (nowTimes >= 3) {
                            //需要继续背
                            nowDay++;
                            editer.putInt("nowday" + taskId, nowDay);
                            nowTimes = 0;
                            editer.putInt("nowtimes" + taskId, nowTimes);
                            nowIndex = 0;
                            editer.putInt("nowIndex" + taskId, nowIndex);
                            editer.commit();
                        }
                        editer.putInt("nowtoday" + taskId, today);
                        nowToday = today;
                    }
                }
                TaskInfo taskInfo = new TaskInfo();
                if (dayCount == 0) {
                    return null;
                }
                taskInfo.start = start;
                taskInfo.dayCount = dayCount;
                taskInfo.nowDay = nowDay;
                taskInfo.nowIndex = nowIndex;
                taskInfo.nowToday = nowToday;
                taskInfo.nowTimes = nowTimes;
                return taskInfo;
            }
            return null;
        }

        private static TaskInfo getTaskInfoFromDB(int taskId) {
            SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(DBManage.DB_PATH + "/" + DBManage.DB_NAME, null);
            String sql = "select name,count  from book  where id = " + taskId;
            TaskInfo taskInfo = new TaskInfo();
            Cursor cursor = database.rawQuery(sql, null);
            if (cursor != null) {
                cursor.moveToFirst();
                if (!cursor.isAfterLast()) {
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    int count = cursor.getInt(cursor.getColumnIndex("count"));
                    taskInfo.taskName = name;
                    taskInfo.wordCount = count;
                    taskInfo.taskId = taskId;
                    cursor.moveToNext();
                }
                cursor.close();
            }
            database.close();
            return taskInfo;
        }

    }

}
